// pages/carbrand/carbrand.js
const app = getApp();
//获取应用实例
let codeOK = 0;
let imageUrl = app.globalData.imageUrl;
// console.log(url);
Page({
  /**
   * 页面的初始数据
   */
  data: {
    dailogmodel: false,
    dailogmodel2: false,
    isScroll: true,//控制底层滚动条
    dianjisou: false,//控制取消按钮
    searchinput: '',//默认数据
    s_p: 1,
    s_flag: true,
    toView: 'inToViewA',
  },

  //获取车辆品牌列表
  getCarBrand: function () {
    var url = '/service/carBrand/findCarBrandList';
    console.log(url);
    var _this = this;
    app.post(url, null, function(res) {
      console.log(res);
      if (res.code == codeOK) {
        var indexList = [];
        var CarBrand = [];
        var pre = '';
        var carBrandItems = [];

        for (var i in res.data) {
          var item = new Object();
          item.id = res.data[i].id;
          item.logo = imageUrl + res.data[i].logo;
          item.carBrand = res.data[i].carBrand;
          if (pre != res.data[i].prefix) {
            carBrandItems = new Array();
            pre = res.data[i].prefix;
            item.pre = pre;
            carBrandItems.push(item);
            indexList.push(pre);
            CarBrand.push(carBrandItems);
          } else {
            carBrandItems.push(item);
          }
        }
        console.log(CarBrand);
        // console.log(indexList);
        _this.setData({
          indexList: indexList,
          CarBrand: CarBrand
        })
          wx.setStorageSync('carBrandList', {
              indexList: indexList,
              CarBrand: CarBrand
          })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // this.getHotCarBrand();
      let carBrandList = wx.getStorageSync('carBrandList');
      if (carBrandList){
          this.setData({
              indexList: carBrandList.indexList,
              CarBrand: carBrandList.CarBrand
          })
      }else{
        this.getCarBrand();
      }

  },
  //跳转排量
  xuanpl: function (e) {
    //console.log(e);
    var id = e.currentTarget.dataset.id;
    var user_id = wx.getStorageSync('user_id');
    var url_r = url + '/index/index/addUserCarModel';
    var _this = this;
    app.fetch1(url_r, { id: id, user_id: user_id }, (err, data) => {
      if (data.code == ERR_OK) {
        wx.redirectTo({
          url: '/pages/models_p/models_p?user_car_id=' + data.user_car_id + '&id=' + id,
        })
      }
    })
  },
 
  //选择品牌
  selectCarBrand: function (e) {
      let carInfo = e.target.dataset.carinfo;
      console.log(e.target.dataset);
      //临时保存选择的车辆品牌信息
      app.tempData.carBrandInfo = carInfo;
    // return;
      wx.navigateTo({
        url: '/pages/addcarpage/addcarpage?method=selectCarBrand',
    })
  },
  //点击锚跳转
  scrollToViewFn: function (e) {
    var _id = e.target.dataset.id;
    this.setData({
      toView: 'inToView' + _id
    })
    //console.log(this.data.toView)

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})